'use strict';
// import { v3 as uuidv3 } from 'uuid';

// import { v4 as uuidv4 } from 'uuid';
const { v4: uuidv4 } = require('uuid');

const Controller = require('egg').Controller;
class BookController extends Controller {
  // 测试接口
  async test() {
    // console.log(v3());
    const { ctx } = this;
    const data = await ctx.service.bookService.test();
    ctx.body = data;
  }
  // 添加图书信息
  async addBook() {
    const { ctx } = this;
    let data = '';
    const bookName = ctx.request.body.bookName;
    const bookPrice = ctx.request.body.bookPrice;
    const bookCover = ctx.request.body.bookCover;
    const imageList = ctx.request.body.imageList;
    const author = ctx.request.body.author;
    const bookPress = ctx.request.body.bookPress;
    const bookInfo = ctx.request.body.bookInfo;
    if (!bookName) {
      ctx.body = { code: 501, msg: '缺少必填项;书籍名称为必填项' };
    } else if (!bookCover) {
      ctx.body = { code: 501, msg: '缺少必填项;书籍封面为必填项' };
    } else {
      data = await ctx.service.bookService.addBook({
        isbn: uuidv4(),
        bookName,
        bookPrice,
        bookCover,
        imageList,
        author,
        bookPress,
        bookInfo,
      });
      ctx.body = { code: 200, data };
    }
  }
  // 删除图书
  async deleteBook() {
    const { ctx } = this;
    const { isbn } = ctx.params;// 获取delete的数据
    if (!isbn) {
      ctx.body = { code: 501, msg: '缺少必填字段;请输入图书的ISBN!' };
    } else {
      const data = await ctx.service.bookService.deleteBook(isbn);
      // 注意：在删除图书时，我们还需要删除关于该书的全部借阅记录
      ctx.body = { code: 200, data };
    }
  }
  // 修改图书
  async updateBook() {
    const { ctx } = this;
    let data = '';
    const isbn = ctx.request.body.isbn;
    const bookName = ctx.request.body.bookName;
    const bookPrice = ctx.request.body.bookPrice;
    const bookCover = ctx.request.body.bookCover;
    const imageList = ctx.request.body.imageList;
    const author = ctx.request.body.author;
    const bookPress = ctx.request.body.bookPress;
    const bookInfo = ctx.request.body.bookInfo;
    if (!isbn) {
      ctx.body = { code: 501, msg: '缺少必填字段;请输入isbn字段!' };
    } else {
      data = await ctx.service.bookService.updateBook({
        isbn,
        bookName,
        bookPrice,
        bookCover,
        imageList,
        author,
        bookPress,
        bookInfo,
      });
      ctx.body = { code: 200, data };
    }
  }
  // 根据isbn查询图书信息
  async findByIsbn() {
    const { ctx } = this;
    const isbn = ctx.request.body.isbn;
    if (!isbn) {
      ctx.body = { code: 501, msg: '缺少必填字段;请输入isbn字段!' };
    } else {
      const data = await ctx.service.bookService.findByIsbn(isbn);
      ctx.body = { code: 200, data };
    }
  }
  // 根据书名查找图书信息
  async findByBookName() {
    const { ctx } = this;
    const bookName = ctx.request.body.bookName;
    if (!bookName) {
      ctx.body = { code: 501, msg: '缺少必填字段;请输入bookName字段!' };
    } else {
      const data = await ctx.service.bookService.findByBookName(bookName);
      ctx.body = { code: 200, data };
    }
  }
  // 查询全部图书信息
  async findAllBook() {
    const { ctx } = this;
    const data = await ctx.service.bookService.findAllBook();
    ctx.body = { code: 200, data };
  }
  // 分页查询图书
  async limitBook() {
    const { ctx } = this;
    const { pagenumber, limit } = ctx.query;
    if (!pagenumber) {
      ctx.body = { code: 501, msg: '缺少必填参数,请输入pagenumber字段' };
    } else if (!limit) {
      ctx.body = { code: 501, msg: '缺少必填参数,请输入limit字段' };
    } else {
      const data = await ctx.service.bookService.limitBook(pagenumber, limit);
      ctx.body = { code: 200, data };
    }
  }
}

module.exports = BookController;
